package com.byzk.p2p.admin.system.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.system.dao.MenuDao;
import com.byzk.p2p.admin.system.dto.GroupRefMenu;
import com.byzk.p2p.admin.system.dto.Menu;
import com.byzk.p2p.admin.system.vo.MenuItem;
import com.byzk.p2p.admin.system.vo.MenuVo;

@Service
public class MenuService {

	@Autowired
	private MenuDao menuDao;
	
	public List<MenuItem> getMenusByGroupId(Integer id){
		if(id==-1){
			List<Menu> list=menuDao.listAllMenu();
			List<MenuItem> meuns=new ArrayList<MenuItem>();
			for (Menu menu : list) {
				if(menu.getPid()==-1){
					List<MenuItem> children=new ArrayList<MenuItem>();
					MenuItem item=menu.convertItem(menu);
					
					for (Menu child : list) {
						if(child.getPid()==menu.getId()){
							children.add(child.convertItem(child));
						}
					}
					item.setChildren(children);
					meuns.add(item);
				}
			}
			return meuns;				
		}else{
			List<Menu> list=menuDao.getMenusByGroupId(id);
			List<MenuItem> meuns=new ArrayList<MenuItem>();
			for (Menu menu : list) {
				if(menu.getPid()==-1){
					List<MenuItem> children=new ArrayList<MenuItem>();
					MenuItem item=menu.convertItem(menu);
					
					for (Menu child : list) {
						if(child.getPid()==menu.getId()){
							children.add(child.convertItem(child));
						}
					}
					item.setChildren(children);
					meuns.add(item);
				}
			}
			return meuns;			
		}

	}
	
	public Integer insertGroupRefMenu(List<GroupRefMenu> groupRefMenus){
		return menuDao.insertGroupRefMenu(groupRefMenus);
	}
	
	public Integer deleteGroupRefMenu(Integer groupId){
		return menuDao.deleteGroupRefMenu(groupId);
	}
	
	public List<GroupRefMenu> getGroupRefMenu(Integer groupId){
		return menuDao.getGroupRefMenus(groupId);
	}
	
	/**
	 * 获取菜单列表
	 * @param queryMap
	 * @param index
	 * @param limit
	 * @return
	 */
	public PageInfo<MenuVo> menuList(Map<String, Object> queryMap,Integer index,Integer limit){
		if (index != null && limit != null) {
			if (queryMap == null) {
				queryMap = new HashMap<String, Object>();
			}
			queryMap.put("start", (index-1)*limit);
			queryMap.put("limit", limit);
		}
		Integer total = menuDao.count(queryMap);
		List<MenuVo> list = menuDao.menuList(queryMap);
		PageInfo<MenuVo> pageInfo = new PageInfo<MenuVo>(total, index, limit);
		pageInfo.setPageList(list);
		return pageInfo;
	}
	
	/**
	 * 获取所有的菜单
	 * @return
	 */
	public List<MenuVo> getAllMenu(){
		return menuDao.all();
	}
	
	/**
	 * 获取菜单
	 * @param menuId 菜单id
	 * @return
	 */
	public MenuVo getMenu(Integer menuId){
		return menuDao.getMenu(menuId);
	}
	
	/**
	 * 保存菜单
	 * @param menu 菜单
	 * @return
	 */
	public Integer saveMenu(Menu menu){
		if (menu.getId() == null) {
			return menuDao.insert(menu);
		}else{
			return menuDao.update(menu);
		}
	}
}
